package com.qnw.CardGroupManagement.service;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AbsListView.*;
import android.widget.AdapterView.*;
import com.qnw.CardGroupManagement.*;
import com.qnw.CardGroupManagement.adapter.*;
import com.ygo.feihua.*;
import com.ygo.feihua.service.*;
import com.ygo.feihua.util.*;
import java.util.*;

import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;

public class ServiceDuelAssistant extends Service
{
	Gongju gj;
	DialogUtils du;

	private WindowManager.LayoutParams wmParams;

	private WindowManager mWindowManager;

	private LinearLayout mFloatLayout;

	private TextView ds_text;
	private String password;  
	private CardListAdapter cladp;
	private List<Card> lc;

	private Button ds_join;

	private Button ds_qx;
	
	@Override
	public IBinder onBind(Intent p1)
	{
		// TODO: Implement this method
		return null;
	}

	@Override
	public void onCreate()
	{
		// TODO: Implement this method
		super.onCreate();
		gj=Gongju.getdx(this);
		lc=new ArrayList<Card>();
		cladp=new CardListAdapter(this,lc);
		du=DialogUtils.getdx(this);
		
		final ClipboardManager cm=(ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		createFloatView();
		cm.addPrimaryClipChangedListener(new  ClipboardManager.OnPrimaryClipChangedListener() {


				@Override  
				public void onPrimaryClipChanged()
				{  
					final String ss=cm.getText().toString().trim();
				/*	final int ssi=ss.indexOf("卡查");
					if (ssi != -1)
					{
						cxCard(ss, ssi);
					}else{*/
						int start=-1;
						String[] sst={"M,","T,","PR,","M#","T#","PR#"};		
						for (String st:sst){
							start = ss.indexOf(st);
							if (start != -1){				
								break;			
							}
						}		

						if (start != -1)
						{
							joinRoom(ss, start);
						}
					//}
				}


			});  
		
		
	}
	boolean isdis=false;
	
	
	private void joinRoom(String ss,int start)
	{
		if (gj.isygo())
		{		
			password = ss.substring(start, ss.length());
			ds_text.setText("加入房间\"" + password + "\"");
			ds_join.setText("加入");
			if (isdis)
			{
				mWindowManager.removeView(mFloatLayout);	
			}
			mWindowManager.addView(mFloatLayout, wmParams);		
			isdis = true;
			new Handler().postDelayed(new Runnable() {

					@Override
					public void run()
					{
						// TODO Auto-generated method stub
						if (isdis)
						{
							isdis = false;
							mWindowManager.removeView(mFloatLayout);
						}
					}
				}, 2000);

			ds_qx.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						if(isdis){
							isdis=false;
							mWindowManager.removeView(mFloatLayout);

						}
						// TODO: Implement this method
					}
				});
			ds_join.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						if(isdis){
							isdis=false;
							mWindowManager.removeView(mFloatLayout);				
						}
						IntentUtil.duelIntent(ServiceDuelAssistant.this,SharedPreferenceUtil.getDuelIp(),SharedPreferenceUtil.getDuelPort(),SharedPreferenceUtil.getDuelName(),password);
						// TODO: Implement this method
					}
				});


		}
		// TODO: Implement this method
	}
	
	
	private void cxCard(String ss,int ssi)
	{
		final String card=ss.substring(ssi+2,ss.length());

		if(isdis){
			mWindowManager.removeView(mFloatLayout);	
		}
		ds_join.setText("查询");
		ds_text.setText("查询卡片\""+card+"\"");
		ds_join.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{

					lc.clear();
					for(Card c:Gongju.cardQuery(card)){
						lc.add(c);
					}
					cladp.notifyDataSetChanged();
					/*final ListView lv= du.dialogl1(true,"\""+card+"\"的搜索结果",cladp);
					lv.setOnScrollListener(new OnScrollListener(){

							@Override
							public void onScrollStateChanged(AbsListView p1, int p2)
							{
								switch (p2){

									case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://停止滚动
										{
											//设置为停止滚动
											cladp.setScrollState(false);

											int allItemNumber = lv.getChildCount();
											View itemView;
											for(int i = 0 ; i < allItemNumber ; i ++){
												itemView = lv.getChildAt(i);
												if(itemView == null){
													return;
												}
												//int index = lv.getFirstVisiblePosition() + i;
												int index = lv.getFirstVisiblePosition() + i;
												CardListAdapter.ViewHolder viewHolder = (CardListAdapter.ViewHolder) itemView.getTag();
												Gongju.setKt(ServiceDuelAssistant.this,viewHolder.card_kt,((Card)cladp.getItem(index)).getId());

											}


											break;
										}
									case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滚动做出了抛的动作
										{
											//设置为正在滚动
											cladp.setScrollState(true);

											break;
										}

									case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动
										{
											//设置为正在滚动
											cladp.setScrollState(true);
											break;
										}
								}
								// TODO: Implement this method
							}

							@Override
							public void onScroll(AbsListView p1, int p2, int p3, int p4)
							{
								// TODO: Implement this method
							}
						});
					
					lv.setOnItemClickListener(new OnItemClickListener(){

							@Override
							public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
							{
								du.dialogci(false,lc.get(p3).getId());
								// TODO: Implement this method
							}
						});*/


					// TODO: Implement this method
				}
			});
		mWindowManager.addView(mFloatLayout,wmParams);		
		isdis=true;
		new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if(isdis){
						isdis=false;
						mWindowManager.removeView(mFloatLayout);
					}
				}
			}, 2000);
		// TODO: Implement this method
	}
	private void createFloatView()
	{
		wmParams = new WindowManager.LayoutParams();
//获取的是WindowManagerImpl.CompatModeWrapper
		mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
//设置window type
		wmParams.type = android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//设置图片格式，效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
//设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
		//实现悬浮窗到状态栏
		wmParams.flags =WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
					WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
		
//调整悬浮窗显示的停靠位置为左侧置顶
		wmParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角为原点，设置x、y初始值，相对于gravity
		wmParams.x = 0;
		wmParams.y = 0;
//设置悬浮窗口长宽数据
		wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;//WindowManager.LayoutParams.MATCH_PARENT;
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 设置悬浮窗口长宽数据
//wmParams.width = 200;
//wmParams.height = 80;
		LayoutInflater inflater = LayoutInflater.from(getApplication());
//获取浮动窗口视图所在布局
		mFloatLayout = (LinearLayout) inflater.inflate(R.layout.service_duel_assistant, null);
//添加mFloatLayout
		
		 ds_join=(Button) mFloatLayout.findViewById(R.id.ds_join);
		ds_text = (TextView) mFloatLayout.findViewById(R.id.ds_text);
		 ds_qx = (Button) mFloatLayout.findViewById(R.id.ds_qx);
		
		}
	
}
